<?php

/*//. string bob_date_distance(string start, string stop, int flags);
  ../ Calculate the distance between two dates.
  ../ Pass date in the format of:
  ../    "YYYY/MM/DD" (e.g. 2007/05/01)
  ../    "YYYY/MM/DD HH:mm" (e.g. 2007/12/23 16:21)
.//*/

define('BOB_DATEDIST_IN_INT',1);
define('BOB_DATEDIST_OUT_INT',2);
define('BOB_DATEDIST_OUT_STR',4);

define(
	'BOB_DATEDIST_PREG',
	'/([\d]{4})\/([\d]{1,2})\/([\d]{1,2})(?: ([\d]{1,2}):([\d]{1,2})(?::([\d]{1,2}))?)?/'
);

function bob_date_distance($start,$stop,$flags = BOB_DATEDIST_OUT_STR) {

	if($flags & BOB_DATEDIST_IN_INT) {
		$istart = $start;
		$istop = $stop;
	} else {
		preg_match(BOB_DATEDIST_PREG,$start,$mstart);
		preg_match(BOB_DATEDIST_PREG,$stop,$mstop);
		
		bob_date_distance_fill($mstart);
		bob_date_distance_fill($mstop);
		
		$istart = mktime($mstart[4],$mstart[5],$mstart[6],$mstart[2],$mstart[3],$mstart[1]);
		$istop = mktime($mstop[4],$mstop[5],$mstop[6],$mstop[2],$mstop[3],$mstop[1]);
		unset($mstart,$mstop);
	}

	$distance = ($istop - $istart);
	if($flags & BOB_DATEDIST_OUT_INT) {
		return $distance;
	} else {
		return bob_date_distance_string($distance);
	}
}

function bob_date_distance_string($time) {
	if($time >= 31104000) {
		//. years.
		$year = floor($time / 31104000); $time -= $year * 31104000;
		$month = floor($time / 2592000); $time -= $month * 2592000;
		$day = floor($time / 86400); $time -= $day * 86400;
		$hour = floor($time / 3600); $time -= $hour * 3600;
		$minute = floor($time / 60); $time -= $minute * 60;
		return sprintf(
			"%dy %dm %dd %dh %dm %ds",
			$year,$month,$day,$hour,$minute,$time
		);	
	}

	else if($time >= 2592000) {
		//. "months".
		$month = floor($time / 2592000); $time -= $month * 2592000;
		$day = floor($time / 86400); $time -= $day * 86400;
		$hour = floor($time / 3600); $time -= $hour * 3600;
		$minute = floor($time / 60); $time -= $minute * 60;
		return sprintf("%dm %dd %dh %dm %ds",$month,$day,$hour,$minute,$time);	
	}

	else if($time >= 86400) {
		//. days.
		$day = floor($time / 86400); $time -= $day * 86400;
		$hour = floor($time / 3600); $time -= $hour * 3600;
		$minute = floor($time / 60); $time -= $minute * 60;
		return sprintf("%dd %dh %dm %ds",$day,$hour,$minute,$time);	
	}

	else if($time >= 3600) {
		//. hours.
		$hour = floor($time / 3600); $time -= $hour * 3600;
		$minute = floor($time / 60); $time -= $minute * 60;
		return sprintf("%dh %dm %ds",$hour,$minute,$time);
	}
	
	else if($time >= 60) {
			//. minutes.
			$minute = floor($time / 60);
			$time -= $minute * 60;
			return sprintf("%dm %ds",$minute,$time);
		}
	
		else {
			//. mearly seconds.
			return sprintf("%ds",$time);
		}
}

function bob_date_distance_fill(&$data) {
	switch(count($data)) {
		case(4): {
			//. just a date provided.
			$data[6] = $data[5] = $data[4] = 0;
			break;
		}
		case(6): {
			//. date and time.
			$data[6] = 0;
			break;
		}	
		case(7): {
			//. date and time with seconds.
			break;
		}
		default: {
			$data = array(0,0,0,0,0,0,0);
		}
	} return;
}

/*
printf(
	"%s\n",
	bob_date_distance("2000/12/25","2007/12/25")
);
*/

?>